package com.drivers.news.service.impl;


import com.drivers.model.OrderInformation.OrderInformation;
import com.drivers.model.OrderInformation.Vo.Vo;
import com.drivers.model.chatMessage.ChatMessage;
import com.drivers.model.driverAttribute.DriverAttribute;
import com.drivers.model.ordercomplaint.OrderComplaint;
import com.drivers.model.userPersonal.UserPersonal;
import com.drivers.news.mapper.OrderInfoMapper;
import com.drivers.news.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Override
    public Integer showOrderStatus() {
        return orderInfoMapper.showOrderStatus();
    }
    @Override
    public Integer showOrderStatusOver() {
        return orderInfoMapper.showOrderStatusOver();
    }
    @Override
    public List<OrderInformation> showAll() {
        List<OrderInformation> orderInformations = orderInfoMapper.showAll();
        for (OrderInformation orderInformation : orderInformations) {
            String phone = orderInformation.getPhone();
            if (phone!= null && phone.length() >= 4) {
                orderInformation.setPhone(phone.substring(phone.length() - 4));
            } else {
                orderInformation.setPhone("");
            }
        }
        return orderInformations;
    }
    @Override
    public List<OrderInformation> showAllAppeal() {
        List<OrderInformation> orderInformations = orderInfoMapper.showAllAppeal();
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");
        for (OrderInformation orderInformation : orderInformations) {
            String completeTimeStr = orderInformation.getCompleteTime();
            if (completeTimeStr!= null) {
                try {
                    Date date = inputFormat.parse(completeTimeStr);
                    String time = outputFormat.format(date);
                    orderInformation.setCompleteTime(time);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return orderInformations;
    }

    @Override
    public Vo showOrderIdApppeal(String orderId) {
        return orderInfoMapper.showAppealById(orderId);
    }

    @Override
    public Integer addFeedback(OrderComplaint orderComplaint) {
        return  orderInfoMapper.addFeedback(orderComplaint);
    }
    @Override
    public DriverAttribute findSelectDrivers(Integer id){
        return orderInfoMapper.findSelectDrivers(id);
    }
    @Override
    public List<ChatMessage> selectMessages(ChatMessage chatMessage){
        List<ChatMessage> chatMessages = orderInfoMapper.selectMessages(chatMessage);
        return chatMessages;
    }
    @Override
    public UserPersonal findPassengers(Integer id){
        return orderInfoMapper.findPassengers(id);
    }
    @Override
    public List<OrderInformation> showHeatMap() {
        List<OrderInformation> orderInformations = orderInfoMapper.showHeatMap();

        orderInformations.forEach(order->{
            String startingPoint = order.getStartingPoint();
            if (startingPoint!= null) {
                String[] split = startingPoint.split(",");
                order.setStartingPoints(split);
            }

        });


        return orderInformations;

    }

}
